package com.food.eat.impl;

import com.alibaba.fastjson.JSON;
import com.food.eat.mapper.Models;
import com.food.eat.pojo.Item;
import com.food.eat.pojo.ItemTwice;
import com.food.eat.pojo.Model;
import com.food.eat.service.ModelsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * @Author RenPu
 * @Date 2021/10/25 12:03
 * @Version 1.0
 * @Description: 当多张表存在多个重复的表字段，可以通过此方案进行解决。此方案可以避免创建重复表，避免多个表进行关联查询
 **/

@Service
public class ModelsServiceImpl implements ModelsService {

    @Autowired
    private Models models;


    @Override
    public int addMessage() {
        List<Item> list1 = new ArrayList<>();
        list1.add(Item.builder().id(UUID.randomUUID().toString()).name("女帝").value("666￥&").sex("女").build());
        List<ItemTwice> list2 = new ArrayList<>();
        list2.add(ItemTwice.builder().id(UUID.randomUUID().toString()).name("混沌").value("9990￥").age("100大寿").build());
        Model build = Model.builder().id("1").firstDatas(JSON.toJSONString(list1)).levelDatas(JSON.toJSONString(list2)).build();
        return models.insert(build);
    }

    @Override
    public Model queryDetialMessage() {
        Model model1 = models.selectByUnique("1");
        Model build1 = Model.builder().id("1").firstData(JSON.parseArray(model1.getFirstDatas(), Item.class)).levelData(JSON.parseArray(model1.getLevelDatas(), ItemTwice.class)).build();
        //(JSON.parseArray(build.getFirstData(),Item.class)).levelData(JSON.parseArray(build.getLevelData(),Item.class)).build();
        return build1;
    }


    /**
     * 删除方法
     *
     * @param firstDataId
     * @param levelDatalevelDataId
     * @return
     */
    @Override
    public Model updateMessage(String firstDataId, String levelDatalevelDataId) {
        List<Item> items = null;
        List<ItemTwice> itemTwices = null;
        Model mo = models.selectByUnique("1");
        if (StringUtils.isNotBlank(firstDataId)) {
            items = JSON.parseArray(mo.getFirstDatas(), Item.class);
            items.removeIf(item -> item.getId().equals(firstDataId));
            mo.setFirstData(items);
            mo.setFirstDatas(JSON.toJSONString(items));
        }

        if (StringUtils.isNotBlank(levelDatalevelDataId)) {
            itemTwices = JSON.parseArray(mo.getLevelDatas(), ItemTwice.class);
            itemTwices.removeIf(itemTwice -> itemTwice.getId().equals(levelDatalevelDataId));
            mo.setLevelData(itemTwices);
            mo.setLevelDatas(JSON.toJSONString(itemTwices));
        }


        mo.setId(UUID.randomUUID().toString());
        if (StringUtils.isNotBlank(mo.getFirstDatas())) {
            mo.setFirstData(JSON.parseArray(mo.getFirstDatas(), Item.class));
        }
        if (StringUtils.isNotBlank(mo.getLevelDatas())) {
            mo.setLevelData(JSON.parseArray(mo.getLevelDatas(), ItemTwice.class));
        }
        models.insert(mo);
        return mo;
    }
}
